package tacocloud.data;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import tacocloud.dao.Ingredient;

import java.sql.ResultSet;
import java.sql.SQLException;

@Repository
public class JdbcIngredientRepository implements IngredientRepository {

//    调用JdbcTemplate
    private JdbcTemplate jdbc;

    @Autowired
    public JdbcIngredientRepository(JdbcTemplate jdbc){
        this.jdbc=jdbc;
    }

//    接口方法的实现

    /**
     * 查找所有菜单
     * @return
     */
    @Override
    public Iterable<Ingredient> findAll() {
        return jdbc.query(
                "SELECT id,name,type FROM Ingredient",
                this::mapRowToIngredient
        );
    }


    /**
     * 通过id查找菜单
     * @param id
     * @return
     */
    @Override
    public Ingredient findOne(String id) {
        return jdbc.queryForObject(
                "SELECT id,name,type FROM Ingredient where id=?",
                this::mapRowToIngredient,id
        );
    }

    /**
     * 保存一种菜单
     * @param ingredient
     * @return
     */
    @Override
    public Ingredient save(Ingredient ingredient) {
        jdbc.update(
                "INSERT INTO Ingredient(id,name,type) VALUES (?,?,?)",
                ingredient.getId(),
                ingredient.getName(),
                ingredient.getType().toString()
        );
        return ingredient;
    }



    private Ingredient mapRowToIngredient(ResultSet rs, int rowNum) throws SQLException {
        return new Ingredient(
                rs.getString("id"),
                rs.getString("name"),
                Ingredient.Type.valueOf(rs.getString("type"))
        );
    }

}
